<?php
/**
 * MyReservation module main class
 *
 * @access 	public
 * @author 	Valentin
 * @copyright	ResMania 2009 all rights reserved.
 * @link		http://developer.resmania.com/api
 */
class RM_Module_MyReservations extends RM_Module implements RM_Payments_Transaction_Callback
{
    public function  __construct()
    {
        $this->name = 'MyReservations';
    }

		public function disable()
    {
        throw new RM_Exception(RM_Environment::getInstance()->getTranslation()->_('Admin.MyReservations.Main', 'DisableAlert'));    
    }

    /**
     * This method will return node object for main admin menu tree.
     * We override the parent class method to set ListJson action instead of default EditJson
     *
     * @return stdClass
     */
    public function getNode(){
        return null;
    }

    /**
     * We don't want to show config node for Groups module (for now)
     *
     * @return null
     */
    public function getConfigNode()
    {
        return null;
    }

    /**
     * Cancel selected reservation
     *
     * @param RM_Reservation_Row $reservation
     * @throw RM_Exception
     * @return void
     */
    public function cancel(RM_Reservation_Row $reservation)
    {
        if ($reservation->cancel == RM_MyReservation_Status::CANCELED) {
            throw new RM_Exception(RM_Environment::getInstance()->getTranslation(RM_Environment::TRANSLATE_MAIN)->_(
                'Common.MyReservations.Status.Errors',
                'ThisReservationIsAlreadyCanceled'
            ));
        }

        if ($reservation->cancel == RM_MyReservation_Status::PENDING) {
            throw new RM_Exception(RM_Environment::getInstance()->getTranslation(RM_Environment::TRANSLATE_MAIN)->_(
                'Common.MyReservations.Status.Errors',
                'ThisReservationIsAlreadyInPendingStatus'
            ));
        }

        $reservation->cancel = RM_MyReservation_Status::PENDING;
        $reservation->cancel_request = date('Y-m-d H:i:s');
        $reservation->save();
        RM_Notifications_Manager::getInstance()->fire('MyReservationsCancel', $reservation);
    }

    /**
     * Approve cancel for the reservation
     *
     * @param RM_Reservation_Row $reservation
     * @throw RM_Exception
     * @return void
     */
    public function approveCancel(RM_Reservation_Row $reservation)
    {
        if ($reservation->cancel !== RM_MyReservation_Status::PENDING) {
            throw new RM_Exception(RM_Environment::getInstance()->getTranslation(RM_Environment::TRANSLATE_MAIN)->_(
                'Common.MyReservations.Status.Errors',
                'ThisReservationIsNotInPendingStatus'
            ));
        }

        $reservation->cancel = RM_MyReservation_Status::CANCELED;
        $reservation->confirmed = 0; //TODO: Hardcode. This will remove this reservation from availability checking process.
        $reservation->save();
        RM_Notifications_Manager::getInstance()->fire('MyReservationsCanceled', $reservation);
    }

    /**
     * Pay the reservation
     *
     * @param RM_Reservation_Row $reservation
     * @param RM_Payments_Plugin $plugin
     * @param float $value
     * @throw RM_Payments_Transaction_Exception
     * @return void
     */
    public function pay(RM_Reservation_Row $reservation, RM_Payments_Plugin $plugin, $value)
    {
        $plugin->beginTransaction(
            $value,
            $reservation->id,
            $this->_getPaymentDescription($reservation),
            RM_Environment::getInstance()->getRouter()->_(
                'MyReservations_Pay',
                'show',
                array('reservationID' => $reservation->id)
            ),
            RM_Environment::getInstance()->getRouter()->_(
                'MyReservations_Pay',
                'show',
                array('reservationID' => $reservation->id)
            ),
            'RM_Module_MyReservations'
        );
    }

    /**
     * This function will save all related information to database.
     *
     * @param RM_Payments_Transaction_Info $info
     * @return void
     */
    public function paymentTransactionCallback(RM_Payments_Transaction_Info $info)
    {
        $billingModel = new RM_Billing();
        $billingRow = $billingModel->createRow(array(
            'reservation_id' => $info->reservationID,
            'total_paid' => $info->total
        ));        
        $billingID = $billingRow->save();

        $billingPaymentsModel = new RM_BillingPayments();
        $billingPaymentRow = $billingPaymentsModel->createRow(array(
            'id' => $billingID,
            'provider' => $info->providerName,
            'transaction_id' => $info->id,
            'status' => $info->statusCode,
            'total' => $info->total,
            'transaction_date' => Date("Y-m-d")
        ));
        $billingPaymentRow->save();
    }

    private function _getPaymentDescription(RM_Reservation_Row $reservation)
    {
        return 'Payment for reservation:'.$reservation->id;
    }

    /**
     * Edit selected reservation
     *
     * @param RM_Reservation_Row $reservation
     * @return void
     */
    public function edit(RM_Reservation_Row $reservation)
    {
        $reservation->edit = RM_MyReservation_Status::PENDING;
        $reservation->save();
        RM_Notifications_Manager::getInstance()->fire('MyReservationsEdit', $reservation);
    }     

    public function install()
    {
        parent::install();        
        $this->_assignDefaultValues();                        
    }

    private function _assignDefaultValues()
    {
        $languagesModel = new RM_Languages();
        $emailNotificationModel = new RM_EmailNotifications();
        $eventNames = array(
            "MyReservationsCancel",
            "MyReservationsCanceled",
            "MyReservationsNote"
        );
        $languages = $languagesModel->fetchAll();
        foreach ($eventNames as $eventName) {            
            $emails = $emailNotificationModel->fetchByName($eventName);            
            foreach ($emails as $email){
                foreach ($languages as $language) {
                    if ($language->iso !== 'en_GB') {
                        $iso = $language->iso;
                        $email->$iso = $email->en_GB;
                    }
                }
                $email->save();
            }
        }
    }
}